package Util::Topic::TopicSearch;
use Moose;
use namespace::autoclean;
use LWP::Simple;
use HTML::Parser;

has 'schema' => (
	is => 'rw',
	required => 1,
	isa => 'DBIx::Class::Schema',
);

sub get_topic_count{
	my ($self) = @_;

	return $self->schema->resultset('Topic')->search({'effective_to' => '2999-01-01'},{})->count;
}

sub get_jinghua{
	my ($self) = @_;

	my @topics = $self->schema->resultset('Topic')->search({'is_jinghua' => 'Y'},{});
	return \@topics;
}

sub get_certain_types{
	my ($self, $topic_type_id, $rows) = @_;

	my @topics = $self->schema->resultset('Topic')->search(
			{'topic_type_id' => $topic_type_id},
			{
				order_by => {-desc=>'me.created'},
				page=>1,
				rows =>$rows 
			}
		);
	return \@topics;
}

sub get_topic_count_by_univ_cat{
	my ($self, $univ_id, $cat_id) = @_;
	
	if($univ_id > -1 and $cat_id == -1)
	{
	return $self->schema->resultset('Topic')->search({'effective_to' => '2999-01-01','danwei_id' => $univ_id},{})->count;
	}
	if($univ_id == -1 and $cat_id == -1)
	{
	return $self->schema->resultset('Topic')->search({'effective_to' => '2999-01-01'},{})->count;
	}
	if($univ_id == -1 and $cat_id > -1)
	{
	return $self->schema->resultset('Topic')->search({'effective_to' => '2999-01-01','topic_type_id' => $cat_id},{})->count;
	}
	if($univ_id > -1 and $cat_id > -1)
	{
	return $self->schema->resultset('Topic')->search({'effective_to' => '2999-01-01','danwei_id' => $univ_id, 'topic_type_id' => $cat_id},{})->count;
	}
}

sub get_topic_for_university{
	my ($self, $university_id, $rows) = @_;

	my @topics = $self->schema->resultset('Topic')->search(
			{'university.id' => $university_id, 'effective_to' => '2999-01-01'},
			{
				select => [
					'me.id',
					'me.author_id',
					'me.title',
					'me.body',
					'me.created',
					'me.is_jinghua',
					'me.is_zhiding',
					'me.updated',
					'me.counter',
					{count => 'comments.id', -as=> 'comments_count'}
				],
				as => [qw/
					id	
					author_id	
					title
					body
					created
					is_jinghua
					is_zhiding
					updated
					counter
					comments_count
				/],
				join => [{'users' => 'university'},{'commented_item' => 'comments'}],
				group_by => [qw/me.id/],
				order_by => {-desc=>[qw/ me.is_zhiding me.updated/]},
				page=>1,
				rows =>$rows
			}
		);
	return \@topics;
}

sub get_top_ten_topic_for_today{
	my ($self, $university_id) = @_;

	my @topics;
	my $now = `date --iso-8601=seconds `;
	my $ago = `date --iso-8601=seconds --date="-12 hour"`;
	if($university_id == -1) {
		@topics = $self->schema->resultset('Topic')->search(
			{'effective_to' => '2999-01-01',
			'comments.created' => {'>' => $ago},
			'comments.created' => {'<' => $now}
			},
			{
				'+select' => [
					{count => 'comments.id', -as=> 'comments_count'}
				],
				join => {'commented_item' => 'comments'},
				group_by => [qw/me.id/],
				order_by => {-desc=>[qw/comments_count created/]},
				page=>1,
				rows =>10 
			}
		);
	} else {
		@topics = $self->schema->resultset('Topic')->search(
			{'danwei_id' => $university_id, 
			'effective_to' => '2999-01-01',
			'comments.created' => {'>' => $ago},
			'comments.created' => {'<' => $now}
			},
			{
				'+select' => [
					{count => 'comments.id', -as=> 'comments_count'}
				],
				join => {'commented_item' => 'comments'},
				group_by => [qw/me.id/],
				order_by => {-desc=>[qw/comments_count created/]},
				page=>1,
				rows =>10
			}
		);
	}
	return \@topics;
}

sub topic_search{
	my ($self, $page, $rows) = @_;

	my @topics = $self->schema->resultset('Topic')->search(
			{'effective_to' => '2999-01-01'},
			{
				select => [
					'me.id',
					'me.author_id',
					'me.title',
					'me.body',
					'me.created',
					'me.updated',
					'me.is_jinghua',
					'me.is_zhiding',
					{count => 'comments.id', -as=> 'comments_count'}
				],
				as => [qw/
					id	
					author_id	
					title
					body
					created
					updated
					is_jinghua
					is_zhiding
					comments_count		
				/],
				join => {'commented_item' => 'comments'},
				group_by => [qw/me.id/],
				order_by => {-desc=>[qw/ me.is_zhiding me.updated/]},
				page=>$page,
				rows => $rows
			}
		);
	return \@topics;
}

sub topic_search_by_univ_cat {
	my ($self, $page, $univ_id, $cat_id, $rows) = @_;
	my @topics;	
	if($univ_id > -1 and $cat_id == -1)
	{
		@topics = $self->schema->resultset('Topic')->search(
			{'danwei_id' => $univ_id, 'effective_to' => '2999-01-01'},
			{
				select => [
					'me.id',
					'me.author_id',
					'me.title',
					'me.body',
					'me.danwei_id',
					'me.topic_type_id',
					'me.created',
					'me.updated',
					'me.counter',
					'me.is_jinghua',
					'me.is_zhiding',
					{count => 'comments.id', -as=> 'comments_count'}
				],
				as => [qw/
					id	
					author_id	
					title
					body
					danwei_id
					topic_type_id
					created
					updated
					counter
					is_jinghua
					is_zhiding
					comments_count		
				/],
				join => {'commented_item' => 'comments'},
				group_by => [qw/me.id/],
				order_by => {-desc=>[qw/ me.is_zhiding me.updated/]},
				page=>$page,
				rows => $rows
			}
		);
	}
	if($cat_id > -1 and $univ_id == -1)
	{
		@topics = $self->schema->resultset('Topic')->search(
			{'topic_type_id' => $cat_id,'effective_to' => '2999-01-01'},
			{
				select => [
					'me.id',
					'me.author_id',
					'me.title',
					'me.body',
					'me.danwei_id',
					'me.topic_type_id',
					'me.created',
					'me.updated',
					'me.counter',
					'me.is_jinghua',
					'me.is_zhiding',
					{count => 'comments.id', -as=> 'comments_count'}
				],
				as => [qw/
					id	
					author_id	
					title
					body
					danwei_id
					topic_type_id
					created
					updated
					counter
					is_jinghua
					is_zhiding
					comments_count		
				/],
				join => {'commented_item' => 'comments'},
				group_by => [qw/me.id/],
				order_by => {-desc=>[qw/ me.is_zhiding me.updated/]},
				page=>$page,
				rows => $rows
			}
		);
	}
	if($cat_id > -1 and $univ_id > -1)
	{
		@topics = $self->schema->resultset('Topic')->search(
			{'danwei_id' => $univ_id, 'topic_type_id' => $cat_id,'effective_to' => '2999-01-01'},
			{
				select => [
					'me.id',
					'me.author_id',
					'me.title',
					'me.body',
					'me.danwei_id',
					'me.topic_type_id',
					'me.created',
					'me.updated',
					'me.counter',
					'me.is_jinghua',
					'me.is_zhiding',
					{count => 'comments.id', -as=> 'comments_count'}
				],
				as => [qw/
					id	
					author_id	
					title
					body
					danwei_id
					topic_type_id
					created
					updated
					counter
					is_jinghua
					is_zhiding
					comments_count		
				/],
				join => {'commented_item' => 'comments'},
				group_by => [qw/me.id/],
				order_by => {-desc=>[qw/ me.is_zhiding me.updated/]},
				page=>$page,
				rows => $rows
			}
		);
	}
	if($univ_id == -1 and $cat_id == -1)
	{
		@topics = $self->schema->resultset('Topic')->search(
			{'effective_to' => '2999-01-01'},
			{
				select => [
					'me.id',
					'me.author_id',
					'me.title',
					'me.body',
					'me.danwei_id',
					'me.topic_type_id',
					'me.created',
					'me.updated',
					'me.counter',
					'me.is_jinghua',
					'me.is_zhiding',
					{count => 'comments.id', -as=> 'comments_count'}
				],
				as => [qw/
					id	
					author_id	
					title
					body
					danwei_id
					topic_type_id
					created
					updated
					counter
					is_jinghua
					is_zhiding
					comments_count		
				/],
				join => {'commented_item' => 'comments'},
				group_by => [qw/me.id/],
				order_by => {-desc=>[qw/ me.is_zhiding me.updated/]},
				page=>$page,
				rows => $rows
			}
		);
	}

        my @arr;
        my $parser = HTML::Parser->new(
                        api_version =>3,
                        start_h => [sub {
                                my ($self,$tag, $attr, $dtext, $origtext) = @_;
                                if($tag =~ /^img$/) {
                                        if (defined $attr->{'src'} ){
                                                push(@arr, $attr->{'src'});
                                        }
                                }
                        }
                        , "self,tagname, attr"],
                );
        my %array;
        foreach my $topic (@topics){
                $parser->parse($topic->body);
                $array{$topic->id} = $arr[0] if(defined($arr[0]));
                undef @arr;
        }

	return (\@topics, \%array);
}

sub find_last_upd_user{

	my ($self) = @_;

	my $rows = 20;
	my @users= $self->schema->resultset('User')->search(
			{},
			{
				select => [
					'me.id',
					'me.username',
					'me.photo',
					{max => 'upd_topic.updated', -as => 'last_upd'}
				],
				as => [qw/
					id
					username
					photo
					last_upd
				/],
				join => ['upd_topic'],
				group_by => 'me.id',
				order_by=>{-desc=>"upd_topic.updated"},
				page=>1,
				rows => $rows
			}
		);
	return \@users;
}

sub find{
        my ($self, $topic_id) = @_;
        return $self->schema->resultset('Topic')->find($topic_id);
}

sub get_topics_with_img{
        my ($self, $num) = @_;
	my $ptn = "%img%src%static/images/big/%";
	my @topics = $self->schema->resultset('Topic')->search(
			{'body' => {like => $ptn }},
			{
				page=>1,
				order_by => {-desc=>"created"},
				rows => $num
			}
		);
	my @arr;
	my $parser = HTML::Parser->new(
			api_version =>3,
			start_h => [sub {
				my ($self,$tag, $attr, $dtext, $origtext) = @_;
				if($tag =~ /^img$/) {
					if (defined $attr->{'src'} ){
						push(@arr, $attr->{'src'});
					}
				}
			}
			, "self,tagname, attr"],
		);
	my %array;
	foreach my $topic (@topics){
		$parser->parse($topic->body);
		$array{$arr[0]} = $topic;
		undef @arr;
	}

	return \%array;
	
}

__PACKAGE__->meta->make_immutable;

1;
